﻿
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppTemp
{
    public class SqlSugarDBContext
    {

        public SqlSugarClient Db;

        public SqlSugarDBContext()
        {
            //var conStr1 = "Server=localhost; port=3306; Database=unionmalldddd; uid=root; pwd=123456; Convert Zero Datetime=True";
            var conStr2 = "Server=193.112.113.12;port=3966;Database=unionmallddd;uid=root;pwd=admin123QWE*;Convert Zero Datetime=True";
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                //Config.ConnectionString2
                //"Server=*; port=3699; Database=unionmallddd; uid=root; pwd=*; Convert Zero Datetime=True",
                ConnectionString = conStr2,
                DbType = DbType.MySql,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
                                             //从库
                SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
                     new SlaveConnectionConfig() { HitRate=10, ConnectionString=conStr2 } ,
                     new SlaveConnectionConfig() { HitRate=10, ConnectionString=conStr2 }
                }
            });
            //调式代码 用来打印SQL
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            Db.Ado.CommandTimeOut = 30000;
            //过滤器
            //Db.QueryFilter.Add(new SqlFilterItem()
            //{
            //    FilterValue = filterDb =>
            //    {
            //        //Writable logic
            //        return new SqlFilterResult() { Sql = " main.isDelete=0" };
            //    },
            //    IsJoinQuery = true
            //});
        }

        //3分钟高手示例
        //public SimpleClient<Permissions> Permissions { get { return new SimpleClient<Permissions>(Db); } }
        //public SimpleClient<Role> Role { get { return new SimpleClient<Role>(Db); } }

        //public SimpleClient<User> User { get { return new SimpleClient<User>(Db); } }

        //public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来处理T表的常用操作
    }
}
